package com.bardsoftware.foronuvolo.sfimport;

import java.util.Date;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

class SFDataHandlerImpl extends DefaultHandler {
    private static abstract class CDataReader {
        private StringBuilder myBuilder = new StringBuilder();

        protected abstract void readCData(String s);

        public void append(char[] ch, int start, int length) {
            myBuilder.append(ch, start, length);
        }

        public void process() {
            readCData(myBuilder.toString());
        }
    }
    private final ParsingState myState;
    private CDataReader myCDataReader;

    SFDataHandlerImpl(ParsingState state) {
        myState = state;
    }
    @Override
    public void startElement(
            String uri, String lname, String qname, Attributes attrs) throws SAXException {
        if (!myState.isReady()) {
            return;
        }
        if ("body".equals(qname)) {
            myCDataReader = new CDataReader() {
                public void readCData(String s) {
                    myState.setText(s);
                }
            };
            return;
        }
        if ("date".equals(qname)) {
            myCDataReader = new CDataReader() {
                public void readCData(String s) {
                    try {
                        Long time = Long.parseLong(s);
                        myState.setDate(new Date(time*1000));
                    }
                    catch(NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            };
            return;
        }
        if ("thread_id".equals(qname)) {
            myCDataReader = new CDataReader() {
                public void readCData(String s) {
                    try {
                        Long id = Long.parseLong(s);
                        myState.setDiscussionID(id);
                    }
                    catch(NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            };
            return;
        }
        if ("msg_id".equals(qname)) {
            myCDataReader = new CDataReader() {
                public void readCData(String s) {
                    try {
                        Long id = Long.parseLong(s);
                        myState.setMessageID(id);
                    }
                    catch(NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            };
            return;
        }
        if ("poster_user".equals(qname)) {
            myCDataReader = new CDataReader() {
                public void readCData(String s) {
                    myState.setUserID(s);
                }
            };
            return;
        }
        if ("poster_name".equals(qname)) {
            myCDataReader = new CDataReader() {
                public void readCData(String s) {
                    myState.setUserDisplayName(s);
                }
            };
            return;
        }
        if ("url".equals(qname)) {
            myCDataReader = new CDataReader() {
                public void readCData(String s) {
                    myState.setUrl(s);
                }
            };
            return;
        }
        if ("subject".equals(qname)) {
            myCDataReader = new CDataReader() {
                public void readCData(String s) {
                    myState.setSubject(s);
                }
            };
            return;
        }
        if ("is_followup_to".equals(qname)) {
            myCDataReader = new CDataReader() {
                public void readCData(String s) {
                    if ("0".equals(s)) {
                        myState.setInitialMessage(true);
                    }
                }
            };
            return;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (myCDataReader != null) {
            myCDataReader.append(ch, start, length);
        }
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        if (myCDataReader != null) {
            myCDataReader.process();
            myCDataReader = null;
        }
        if ("forum_post".equals(name)) {
            myState.buildMessage();
        }
    }

    @Override
    public void endDocument() throws SAXException {
        myState.saveRemainingDiscussions();
    }

}
